package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroup;
import com.itheima.dao.ICheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.service.ICheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = ICheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements ICheckGroupService {

    @Autowired
    private ICheckGroupDao checkGroupDao;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPageByCondition(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckGroup> page = checkGroupDao.findPageByCondition(queryString);
        long total = page.getTotal();
        List<CheckGroup> rows = page.getResult();
        return new PageResult(total,rows);
    }

    /**
     * 添加检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        // 添加检查组
        checkGroupDao.add(checkGroup);
        // 检查组和检查项多对多关联
        Integer checkGroupId = checkGroup.getId();
        this.addCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }

    /**
     * 通过id查找
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup = checkGroupDao.findById(id);
        return checkGroup;
    }

    /**
     * 通过检查组id查询检查项id
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List<Integer> checkGroupIds = checkGroupDao.findCheckItemIdsByCheckGroupId(id);
        return checkGroupIds;
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void update(CheckGroup checkGroup, Integer[] checkitemIds) {

        checkGroupDao.update(checkGroup);

        // 删除原有检查组和检查项之间关联
        checkGroupDao.deleteAssociation(checkGroup.getId());
        // 重新建立检查组和检查项多对多关联
        Integer checkGroupId = checkGroup.getId();
        this.addCheckGroupAndCheckItem(checkGroupId,checkitemIds);
    }

    /**
     * 删除检查组
     * @param id
     */
    @Override
    public void deleteById(Integer id) {

        // 删除原有检查组和检查项之间关联
        checkGroupDao.deleteAssociation(id);

        // 判断是否添加到检查套餐
        long count = checkGroupDao.findCountByCheckGroupId(id);
        // 已添加到检查套餐
        if (count > 0){
            throw  new RuntimeException();
        }

        // 删除检查组
        checkGroupDao.deleteById(id);


    }

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> checkGroups = checkGroupDao.findAll();
        return checkGroups;
    }

    // 建立检查组和检查项多对多关联
    public void addCheckGroupAndCheckItem(Integer checkGroupId,Integer[] checkitemIds){
        if (checkitemIds != null & checkitemIds.length > 0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new  HashMap<String,Integer>();
                map.put("checkGroupId",checkGroupId);
                map.put("checkitemId",checkitemId);
                checkGroupDao.addCheckGroupAndCheckItem(map);
            }
        }
    }
}
